// pages/register/register.js
const app = getApp()
const HttpReq = require('../../utils/HttpReq.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
      phonenumber: null,
      checked: false,
      openId: null
    },
    onChange(event) {
      this.setData({
        checked: event.detail,
      });
      console.log(this.data.checked)
    },
    GP(){
      console.log(this.data.checked)
    },

    getPhoneNumber (e) {
      console.log(this.data.checked)
      if(this.data.checked === false){
        wx.showToast({
          title:'请先同意《用户协议》和《隐私协议》',
          icon:'error',//失败的类型
          duration:1500//默认毫秒数为1500，类型是数值
        })
      }
      else{
          
        console.log(e.detail.code)  // 动态令牌
        let code = e.detail.code
        let url = app.globalData.hostIp + '/weixin/getPhoneNumber?code=' + code
        HttpReq.httpsResquest(url,null,'GET').then(res=>{
          console.log(res)
          if(res.code == 1){
            var PN = res.data
            //var PN = '18034356183'
            console.log(res.data)
            this.setData({
              phonenumber: PN
            })
            let url2 = app.globalData.hostIp + "/client/register?openId=" + app.globalData.openId + "&phoneNumber=" + PN.replace(/"/g, '');
            console.log(url2)
            // let url2 = app.globalData.hostIp + '/client/register?openId='+ 'fsdafsdfg' + '&phoneNumber=' + PN
            HttpReq.httpsResquest(url2, null, 'GET').then(res2=>{
              console.log(res2)
              if(res2.code == 1){
                // 注册成功
                console.log(res2.data);
                app.globalData.userInfo = res2.data
                setTimeout(function () {
                  //要延时执行的代码
 
                  wx.reLaunch({
                    url: '/pages/index/index'//页面路径
                  })
                }, 2000) //延迟时间
                wx.showToast({
                  title: '注册完成！',
                  icon:'success',
                  duration: 2000,
                  mask:true,
                })
                
              }
              else{
                wx.showToast({
                  title: '提交失败，请联系管理员解决',
                  icon:'none',
                  duration: 2000
                })
              }
            })
          }
        })
        console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
        console.log(e.detail.errno)  // 错误码（失败时返回）
      }
      
    },

    /**
     * 生命周期函数--监听页面加载
     */
    tee() {
      console.log('哈哈啊')
      wx.navigateTo({
        url: '/pages/my/feedback/index'
      })
    },
    onLoad(options) {
      wx.setNavigationBarTitle({
        title: '诺银会员'
      })
      let that = this
      // 登录
      wx.login({
        success:(res)=>{
          console.log(res);
          let code=res.code
          //通过code换取openid
          let Url = app.globalData.hostIp + '/weixin/getOpenId?code=' + code
          HttpReq.httpsResquest(Url, null, 'GET').then(res=>{
            console.log(res)
            if(res.code == 1){
            //   userInfo.openid=res.data.openid
              //获取到你的openid
              console.log("下面是openid")
              var OI = res.data
              //var OI = 'fsdafgfdgfdgfd'
              console.log(res.data)
              that.setData({
                openId: OI
              })
              getApp().globalData.openId = OI
              let url2 = app.globalData.hostIp + '/client/login?openId=' + OI
              HttpReq.httpsResquest(url2, null, 'GET').then(res1=>{
                console.log(res1.data);
             
                if(res1.code == 1){
                  // 已注册的顾客，直接登录
                
                  wx.reLaunch({
                    url: '/pages/index/index'
                  })
                  console.log("我进来了")
                  app.globalData.userInfo = res1.data
                  wx.setStorageSync('userInfo',res1.data)
                }
              })

            }
          })

  
        }
      })
      
    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})